#coding:utf-8
import appconf
import datetime
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(appconf.app)


class LocustResult(db.Model):
    """存储结果表"""
    __tablename__ = 'locust_result'
    id = db.Column(db.Integer, primary_key=True)

    task_name = db.Column(db.String(100), nullable=True)

    avg_response_time = db.Column(db.Float(11), nullable=True)
    max_response_time = db.Column(db.Float(11), nullable=True)
    median_response_time = db.Column(db.Float(11), nullable=True)
    ninetieth_response_time = db.Column(db.Float(11), nullable=True)

    current_rps = db.Column(db.Float(11), nullable=True)
    user_count = db.Column(db.Integer, nullable=True)
    errors_count = db.Column(db.String(10), nullable=True)

    num_requests = db.Column(db.Integer, nullable=True)
    num_failures = db.Column(db.Integer, nullable=True)
    result_json = db.Column(db.Text, nullable=True)
    last_time = db.Column(db.String(50), nullable=True)

    def __init__(self, task_name, avg_response_time, max_response_time, median_response_time, ninetieth_response_time,
                 current_rps, user_count, errors_count, result_json,num_requests,num_failures):
        self.task_name = task_name
        self.avg_response_time = avg_response_time
        self.max_response_time = max_response_time

        self.median_response_time = median_response_time
        self.ninetieth_response_time = ninetieth_response_time
        self.current_rps = current_rps
        self.user_count = user_count
        self.errors_count = errors_count
        self.result_json = result_json
        self.num_failures = num_failures
        self.num_requests = num_requests

        self.last_time = str(datetime.datetime.now())

    def add(self):
        db.session.add(self)
        db.session.commit()


class LocustNode(db.Model):
    """可用节点表"""
    __tablename__ = 'locust_Node'
    id = db.Column(db.Integer, primary_key=True)

    master_ip = db.Column(db.String(100), nullable=True)
    master_port = db.Column(db.String(7), nullable=True)
    master_web_port = db.Column(db.String(7), nullable=True)
    case_id = db.Column(db.Integer, nullable=True)

    def __init__(self, master_ip, master_port, master_web_port, case_id):
        self.master_ip = master_ip
        self.master_port = master_port
        self.master_web_port = master_web_port
        self.case_id = case_id


class BoomerCase(db.Model):
    """可用节点表"""
    __tablename__ = 'boomer_case'
    id = db.Column(db.Integer, primary_key=True)
    task_name = db.Column(db.String(100), nullable=True)
    case_json = db.Column(db.Text, nullable=True)
    last_time = db.Column(db.String(50), nullable=True)

    def __init__(self, task_name, case_json):
        self.task_name = task_name
        self.case_json = case_json
        self.last_time = str(datetime.datetime.now())

    def add(self):
        db.session.add(self)
        db.session.commit()

class BoomerRemark(db.Model):
    __tablename__ = 'boomer_remark'
    id = db.Column(db.Integer, primary_key=True)
    task_name = db.Column(db.String(100), nullable=True)
    note = db.Column(db.Text, nullable=True)
    last_time = db.Column(db.String(50), nullable=True)

    def __init__(self, task_name, note):
        self.task_name = task_name
        self.note = note
        self.last_time = str(datetime.datetime.now())

    def add(self):
        db.session.add(self)
        db.session.commit()

db.create_all()


